package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.ClassesConfig;
import com.tbit.uqbike.object.pojo.dto.AccountUserWorkConfigDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.ClassesConfigService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.dao.core.ClassesConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hzw
 * @date ：Created in 2021/8/24 13:39
 * @description：班次设置业务层
 * @modified By：
 * @version: $
 */
@Service(value = "classesConfigService")
public class ClassesConfigServiceImpl implements ClassesConfigService {

    @Autowired
    private ClassesConfigDao classesConfigDao;
    @Autowired
    private PurviewService purviewService;

    @Override
    public List<ClassesConfig> getClassesConfig(ClassesConfig classesConfig) {
        Map<String, Object> map = new HashMap<>();
//        if (null!=classesConfig.getStartTime() && null!=classesConfig.getEndTime()){
//            map.put("startTime", classesConfig.getStartTime());
//            map.put("endTime", classesConfig.getEndTime());
//        }
        map.put("accountId", classesConfig.getAccountId());
        map.put("name", classesConfig.getName());
        return classesConfigDao.getClassesConfig(map);

    }

    @Override
    public List<ClassesConfig> getClassesConfigPage(Integer accountId, Integer pageNO, Integer rowCount) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        return classesConfigDao.getClassesConfigPage(map);
    }

    @Override
    public Integer getClassesConfigCount(Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        return classesConfigDao.getClassesConfigCount(map);
    }

    @Override
    public Integer IncreaseClasses(ClassesConfig classesConfig) {
        Map<String, Object> map = new HashMap<>();
        Date time = new Date(System.currentTimeMillis());
        map.put("accountId", classesConfig.getAccountId());
        map.put("name", classesConfig.getName());
        map.put("startTime", classesConfig.getStartTime());
        map.put("endTime", classesConfig.getEndTime());
        map.put("addTime", new Date());
        return classesConfigDao.IncreaseClasses(map);
    }

    @Override
    public Integer delClasses(Integer workNumber, LoginInfo loginInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("workNumber", workNumber);
        List<ClassesConfig> configList = classesConfigDao.getClassesConfig(map);
        if (configList.isEmpty()) {
            throw new BaseException(SystemCodeEnum.CLASSES_NOT_EXIST);
        }
        List<AccountUserWorkConfigDTO> list = classesConfigDao.getAccountWork(workNumber);
        if (!list.isEmpty()) {
            throw new BaseException(SystemCodeEnum.CLASSES_IS_USE);
        }
        for (ClassesConfig config : configList) {
            //权限验证
            if (!purviewService.purviewCheck(loginInfo.id, config.getAccountId())) {
                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
            }
        }
        return classesConfigDao.delClasses(workNumber);
    }

    @Override
    public void upClasses(ClassesConfig classesConfig) {
        Map<String, Object> map = new HashMap<>();
        map.put("workNumber", classesConfig.getWorkNumber());
        map.put("name", classesConfig.getName());
        map.put("startTime", classesConfig.getStartTime());
        map.put("endTime", classesConfig.getEndTime());
        map.put("updateTime", new Date());
        classesConfigDao.upClasses(map);
    }

    @Override
    public List<AccountUserWorkConfigDTO> queryPage(Integer accountId, String workType, Integer workNumber, String phone, List<Integer> regionList, Integer pageNO, Integer rowCount, Integer locationType) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("workType", workType);
        map.put("workNumber", workNumber);
        map.put("phone", phone);
        map.put("regionList", regionList);
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        map.put("locationType", locationType);
        return classesConfigDao.queryPage(map);
    }

    @Override
    public List<AccountUserWorkConfigDTO> queryCount(Integer accountId, String workType, Integer workNumber, String phone) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("workType", workType);
        map.put("workNumber", workNumber);
        map.put("phone", phone);
        return classesConfigDao.queryCount(map);
    }

    @Override
    public void upWorkType(Integer accountId, List<String> typeList, List<Integer> workNumber, Integer accountUserId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("accountUserId", accountUserId);
        map.put("workNumber", workNumber);
        Integer del = classesConfigDao.delWorkType(map);
        for (String type : typeList) {
            String locationType = "0";
            if (type.contains("-")){
                String[] split = type.split("-");
                type = split[0];
                locationType = split[1];
            }
            map.put("workType", type);
            map.put("locationType",locationType);
            classesConfigDao.insertWorkType(map);
        }
    }
}
